using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "GameSO", menuName = "SO/GameSO", order = 1)]
public class GameSO : ScriptableObject
{
    public MapSO[] mapArray;//多个地图对象
    public int selectedMapID=-1;//当前地图ID
    public int selecteLevelID=-1;//当前关卡ID

    public void UpdateLevel(int number)
    {
        if(number <= 0) return;
        
        //判断是否超过最大关卡
        if (number > mapArray[selectedMapID-1].startNumnberOfLevel[selecteLevelID-1])
        {
            //更新关卡
            mapArray[selectedMapID-1].startNumnberOfLevel[selecteLevelID-1] = number;
            
            //更新地图的开始关卡
            int sum = 0;
            foreach (int num in mapArray[selectedMapID-1].startNumnberOfLevel)
            {
                if (num > 0)
                {
                    sum += num;//累计关卡数
                }
            }
            mapArray[selectedMapID-1].startNumnberOfMap = sum;//更新星星数
            
            //判断当前地图是否是最后一关
            if (selectedMapID<mapArray.Length && selecteLevelID >= mapArray[selectedMapID-1].startNumnberOfLevel.Length)
            {
                if (mapArray[selectedMapID].startNumnberOfMap == -1)//判断下一地图没有开始
                {
                    mapArray[selectedMapID].startNumnberOfMap = 0;//解锁下一地图
                    mapArray[selectedMapID].startNumnberOfLevel[0] = 0;//解锁下一关
                }
            }
            else
            {
                //判断下一关是否开启
                if (mapArray[selectedMapID - 1].startNumnberOfLevel[selecteLevelID] == -1)
                {
                    mapArray[selectedMapID - 1].startNumnberOfLevel[selecteLevelID] = 0;//解锁下一关
                }
            }
        }
    }
}
